import { _decorator, Component, Node, UITransform, randomRange, v3, view, instantiate, Vec3 } from 'cc';
import { Battle } from '../Battle';
import { EnDynamicLineType } from '../Common/const/EnCommon';
import { EnEvent } from '../Common/const/EnEvent';
import { GameAPI } from '../Common/GameAPI';
import { Globals } from '../Globals';
const { ccclass, property } = _decorator;

export class ElementChildren {
    public posY: number;
    public obj: Node;
}

@ccclass('TowerGameManager')
export class TowerGameManager extends Component {

    @property(Node)
    private contentNode: Node = null;
    @property(Node)
    private titlePanel: Node = null;
    @property(Node)
    private pointPrefab: Node = null;

    private elementList: Array<ElementChildren> = [];

    start() {
        GameAPI.RegGEvent(EnEvent.BeginBossCreateEvent, this.OnBeginBossCreateEvent.bind(this), this);
        this.scheduleOnce(function () {
            GameAPI.FireGEvent(EnEvent.GuideTipEvent, { index: 1, active: true });
            GameAPI.FireGEvent(EnEvent.GuideTipEvent, { index: 2, active: true });
            GameAPI.FireGEvent(EnEvent.DynamicLineEvent, { type: EnDynamicLineType.InitTree });
        }.bind(this), 1);
        this.InitElementChildren();
    }

    CreatorPoint(enemyNode: Node, prefab, pos) {
        let obj = instantiate(prefab);
        enemyNode.addChild(obj);
        obj.setPosition(pos);
        // obj.active = true;
        // obj.name = "Point";
    }

    InitElementChildren() {
        for (let index = 0; index < this.contentNode.children.length; index++) {
            const element = this.contentNode.children[index];
            let info = new ElementChildren();
            info.posY = element.position.y;
            info.obj = element;
            this.elementList.push(info);
            if (element.name.includes("Tree")) {
                this.CreatorPoint(element, this.pointPrefab, new Vec3(-17, -78, 0));
            }
        }
        // 按 score 从大到小排序
        const sortedData = this.elementList.sort((a, b) => b.posY - a.posY);
        let childrenIndex = 0;
        sortedData.forEach(item => {
            this.contentNode.removeChild(item.obj);
            this.contentNode.insertChild(item.obj, childrenIndex);
            childrenIndex++;
        });
    }

    onDestroy() {
        GameAPI.OffAllCaller(this);
    }

    OnBeginBossCreateEvent(arg) {
        let bossNode = null;
        if (arg == 1) {
            bossNode = Globals.getNode("Boss1", Battle.ContentParent);
        }
        else if (arg == 2) {
            bossNode = Globals.getNode("Boss2", Battle.ContentParent);
        }
        else if (arg == 3) {
            bossNode = Globals.getNode("Boss3", Battle.ContentParent);
            this.titlePanel.active = true;
        }
        if (bossNode) {
            this.BossBornPos(bossNode);
        }
    }

    BossBornPos(bossNode) {
        const screenHeight = view.getVisibleSize().height;
        const screenWidth = view.getVisibleSize().width;
        // 相机的位置
        const cameraWorldPos = Battle.Camera.getWorldPosition();
        // 相机边界
        const left = cameraWorldPos.x - screenWidth / 2;
        const right = cameraWorldPos.x + screenWidth / 2;
        const top = cameraWorldPos.y + screenHeight / 2;
        const bottom = cameraWorldPos.y - screenHeight / 2;
        // 地图边界
        const MapNode = Battle.MapNode;
        const mapleft = MapNode.getWorldPosition().x - MapNode.getComponent(UITransform).contentSize.width / 2;
        const mapright = MapNode.getWorldPosition().x + MapNode.getComponent(UITransform).contentSize.width / 2;
        const maptop = MapNode.getWorldPosition().x + MapNode.getComponent(UITransform).contentSize.height / 2;
        const mapbottom = MapNode.getWorldPosition().x - MapNode.getComponent(UITransform).contentSize.height / 2;
        // 随机敌人在相机上下左右
        const direction = Math.floor(randomRange(0, 4)); //0，1，2，3
        //敌人位置
        let enemyPos = v3();
        const offset = 50; //偏移量，远离相机距离
        switch (direction) {
            case 0: //上面
                enemyPos.x = randomRange(left, right);
                enemyPos.y = top + offset;
                break;
            case 1: //下面
                enemyPos.x = randomRange(left, right);
                enemyPos.y = bottom - offset;
                break;
            case 2: //左边
                enemyPos.x = left - offset;
                enemyPos.y = randomRange(top, bottom);
                break;
            case 3: //右边
                enemyPos.x = right + offset;
                enemyPos.y = randomRange(top, bottom);
                break;
        }
        // 确保在地图内(Math.min 限制在不超过右边界，Math.max限制不超过左边界)
        enemyPos.x = Math.max(mapleft, Math.min(enemyPos.x, mapright));
        enemyPos.y = Math.max(mapbottom, Math.min(enemyPos.y, maptop));

        bossNode.setWorldPosition(enemyPos);
    }

}


